
const App = getApp()

let { AJAX, timeTranfer, imgUrl } = require("../../../utils/util.js")
Page({
	/**
	 * 页面的初始数据
	 */
  data: { 
    navH: '',
    proid:'',
    detail: [],
    videoList: [],
    page: 1,
    total: 0,
     play:0,
    imgUrl: imgUrl
  }, 

	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    this.setData({
      navH: App.globalData.navHeight,
      proid: options.id
    })
    
     if (!wx.getStorageSync("ACCESSTOKEN")) {
        App.userInfoReadyCallback = res => {
           wx.setStorageSync('ACCESSTOKEN', res);
           this.requestDetail()
           this.requestList()
        }
        return
     }
    this.requestDetail()
    this.requestList()
  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },
   clickitem: function (e) {
      console.log(e)
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
         url: '/pages/VideoLibrary/videoDetail/videoDetail?id=' + id,
      })
   },
	/**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {
    let vm = this
    if (vm.data.videoList.length < vm.data.total) {
      vm.setData({
        page: vm.data.page + 1
      })
      vm.requestList(true)
    }
  },
  /**
   * 列表
   * idea/list
   */
  requestList: function (str) {
    let vm = this
    AJAX("video/index",'GET',{
      page: vm.data.page,
       id: vm.data.proid
    }).then(res => { 
      let _data = []
      if (str) {
        _data = vm.data.videoList
      }
      if (res.data) {
        res.data.forEach(function (item, index) {
          item.created_at = timeTranfer(item.created_at, 1)
          _data.push(item)
        })
      }     
      vm.setData({
        videoList: _data,
        total: res.header["X-Pagination-Total-Count"]
      })
    })
  },

	/***
	 * 详情
	 */
  requestDetail: function () {    
    let vm = this
    AJAX("video/detail", "GET", {
      id: vm.data.proid
    }).then(res => {
      console.log(res.data)
      res.data.created_at = timeTranfer(res.data.created_at, 1)
      res.data.updated_at = timeTranfer(res.data.updated_at, 1)     
      
      vm.setData({
        detail: res.data
      })
    })
  },

	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  },
  navBack() {
    wx.navigateBack()
  }
})